package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeReplyEnrollService;


import com.bidevalution.dao.entity.BeReplyEnroll;
import com.bidevalution.dao.entity.BeSectionParticipant;
import com.bidevalution.dao.mapper.BeReplyEnrollMapper;
import com.google.common.collect.Maps;
import com.qtp.core.model.Paging;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @description:
 * @author: xiayf
 * @date: 2019-06-11 11:58
 * 答辩单位信息业务层接口实现类
 */
@Service
public class BeReplyEnrollServiceeImpl implements BeReplyEnrollService {
    @Autowired
    private BeReplyEnrollMapper beReplyEnrollMapper;

    @Override
    public boolean insertBeReplyEnroll(List<BeSectionParticipant> list, String replyId) throws Exception {
        
        BeReplyEnroll beReplyEnroll = new BeReplyEnroll();
        beReplyEnroll.setReplyId(replyId);
        //遍历报名单位信息并写入
        int i = 0;
        //获取最大序列号
        Integer maxOrder = getMaxReplayOrder();
        for (BeSectionParticipant participant : list) {
            i++;
            beReplyEnroll.setSeqId(IdsUtil.uuid());
            beReplyEnroll.setEnterpriseId(participant.getParticipantId());
            beReplyEnroll.setEnterpriseName(participant.getParticipantName());
            beReplyEnroll.setCreditCode(participant.getCreditCode());
            beReplyEnroll.setLeaderName(participant.getManager());
            beReplyEnroll.setLeaderCard(participant.getManagerCode());
            beReplyEnroll.setCreateTime(DateUtil.now());
            beReplyEnroll.setUpdateTime(DateUtil.now());
            maxOrder += i;
            beReplyEnroll.setReplyOrder(maxOrder);
            if (beReplyEnrollMapper.insertBeReplyEnroll(beReplyEnroll) < 1) {
                return false;
            }
        }
        RedisUtil.set("MAX_REPLY_ORDER", maxOrder.toString());
        return true;
    }

    @Override
    public boolean deleteByReplyId(String replyId) throws Exception {
        
        if (beReplyEnrollMapper.deleteByReplyId(replyId) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Paging<List<BeReplyEnroll>> selectReplyEnrollList(Map<String, Object> paramsMap) throws Exception {
        // 获取第一个数据源
        
        // 获取分页参数
        int limit = OConvertUtils.getInt(paramsMap.get("rp"), 10);
        int page = OConvertUtils.getInt(paramsMap.get("page"), 1) - 1;
//        Page<BeReplyEnroll> BeReplyEnrollPage = PageHelper.startPage(page, limit);
        String replyId = OConvertUtils.getString(paramsMap.get("replyId"));
        List<BeReplyEnroll> list = beReplyEnrollMapper.selectReplyEnrollList(replyId,limit,page);
        return new Paging(beReplyEnrollMapper.getCount(replyId), list);
    }

    @Override
    public boolean updateIdentityState(String creditCodes, String replyId) throws Exception {
        
        if (beReplyEnrollMapper.updateIdentityState(creditCodes, replyId) > 0) {
            return true;
        }
        return false;
    }
	@Override
	public List<BeReplyEnroll>  selectByEnterpriseCode(String orgCode,String sectionId) {
		 
		return beReplyEnrollMapper.selectListByOrgCode(orgCode,sectionId);
	}

	@Override
	public BeReplyEnroll selectById(String seqId) {
		// TODO Auto-generated method stub
		 
		return beReplyEnrollMapper.selectByPrimaryKey(seqId);
	}

	@Override
	@Transactional
	public void updateIdentity(BeReplyEnroll beReplyEnroll) throws Exception {
		// TODO Auto-generated method stub
		 
		beReplyEnrollMapper.updateByPrimaryKeySelective(beReplyEnroll);
	}


    @Override
    public List<BeReplyEnroll> getIdentityEnrollList(String replyId) throws Exception {
        
        return beReplyEnrollMapper.getIdentityEnrollList(replyId);
    }

    @Override
    public BeReplyEnroll selectByPrimaryKey(String seqId) throws Exception {
        
        return beReplyEnrollMapper.selectByPrimaryKey(seqId);
    }
	@Override
	public BeReplyEnroll selectByStreamId(String streamId) {
		// TODO Auto-generated method stub
		
		return beReplyEnrollMapper.selectByStreamId(streamId);
	}

    /**
     *  获得当前最大的序列号
     * @return
     */
    private Integer getMaxReplayOrder() throws Exception {
        String replyOrderMax = RedisUtil.get("MAX_REPLY_ORDER");
        if (StringUtils.isNotBlank(replyOrderMax)) {
            return Integer.parseInt(replyOrderMax);
        }
        // 从数据库中获得最大的流水号
        String sNo = beReplyEnrollMapper.getMaxReplayOrder();
        if (StringUtils.isBlank(sNo)) {
            // 若数据库中不存在，则默认从1开始
            sNo = "1";
        }
        return Integer.parseInt(sNo);
    }

	@Override
	public long selectCountByReplyId(String replyId) {
		// TODO Auto-generated method stub
        
		return beReplyEnrollMapper.selectCountByReplyId(replyId);
	}

    @Override
    public int getCountByIsPublish(String replyId, String isPublish) {
        
        return beReplyEnrollMapper.getCountByIsPublish(replyId, isPublish);
    }
}
